package com.arpa.ntocc.common.service.impl;

import com.arpa.ntocc.common.common.cache.PartyCache;
import com.arpa.ntocc.common.common.util.CommonUtil;
import com.arpa.ntocc.common.common.util.UserUtil;
import com.arpa.ntocc.common.domain.dto.PartyGroupDTO;
import com.arpa.ntocc.common.domain.entity.Party;
import com.arpa.ntocc.common.domain.entity.PartyGroup;
import com.arpa.ntocc.common.domain.enums.PartyTypeEnum;
import com.arpa.ntocc.common.domain.vo.PartyGroupVO;
import com.arpa.ntocc.common.mapper.PartyGroupMapper;
import com.arpa.ntocc.common.service.IPartyGroupService;
import com.arpa.ntocc.common.service.IPartyResourceService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网点表 服务实现类
 * </p>
 *
 * @author wangyujie
 * @since 2020-01-06
 */
@Service
@Log4j2
public class PartyGroupServiceImpl extends ServiceImpl<PartyGroupMapper, PartyGroup> implements IPartyGroupService {


    @Resource
    private IPartyResourceService partyResourceService;

    @Resource
    private PartyCache partyCache;





    /**
     * 添加组织信息
     *
     * @param entity 组织信息
     * @return boolean
     * @author liuxr
     * @date 2021/11/12 14:07
     */
    @Override
    public boolean add(PartyGroup entity) {
        //TODO 根据后续逻辑是否创建party数据
        Party party = new Party();
        party.setCode(entity.getCode());
        party.setName(entity.getGroupName());
        party.setPartyType(PartyTypeEnum.GROUP);


        String loginUserCode = UserUtil.getCode();
        String loginUserName = partyCache.translate(loginUserCode);
        entity.setCreatedBy(loginUserCode);
        entity.setModifiedBy(loginUserCode);
        entity.setCreatedName(loginUserName);
        entity.setCreatedName(loginUserName);
        return super.save(entity);
    }




    /**
     * 根据code更新实体
     *
     * @param entity
     * @return
     */
    @Override
    public boolean updateByCode(String code,PartyGroup entity) {
        String loginUserCode = UserUtil.getCode();
        String loginUserName = partyCache.translate(loginUserCode);
        entity.setModifiedBy(loginUserCode);
        entity.setCreatedName(loginUserName);
        return update(entity, Wrappers.lambdaUpdate(PartyGroup.class).eq(PartyGroup::getCode, code));
    }


    /**
     * 查询列表
     *
     * @param partyGroupDTO
     * @return
     */
    @Override
    public List<PartyGroupVO> queryList(PartyGroupDTO partyGroupDTO) {
        //排序字段名需要驼峰转数据库下划线类型字段名
        if (StringUtils.isNotEmpty(partyGroupDTO.getSortField())) {
            partyGroupDTO.setSortField(CommonUtil.camel2Underline(partyGroupDTO.getSortField()));
        }
        return baseMapper.queryList(partyGroupDTO);
    }

    /**
     * 查询合计,包含总数
     *
     * @param partyGroupDTO
     * @return
     */
    @Override
    public PartyGroupVO queryListSum(PartyGroupDTO partyGroupDTO) {
        return baseMapper.queryListSum(partyGroupDTO);
    }





    /**
     * 查询默认机构
     *
     * @return
     */
    @Override
    public PartyGroup getDefault() {

        return null;
    }

    @Override
    public PartyGroup getByCode(String code) {
        return baseMapper.selectOne(new QueryWrapper<PartyGroup>().lambda().eq(PartyGroup::getCode, code));
    }

    @Override
    public List<Map<String, String>> getGroupWarehouse(String groupCode) {
        return baseMapper.getGroupWarehouse(groupCode);
    }
}
